package com.jhgsys.internal.incentive.service.impl;

import com.alibaba.fastjson.JSON;
import com.jhgsys.internal.incentive.entity.IncentiveRewardClauseRule;
import com.jhgsys.internal.incentive.entity.IncentiveRewardClauseRuleRecord;
import com.jhgsys.internal.incentive.mapper.IncentiveRewardClauseRuleMapper;
import com.jhgsys.internal.incentive.service.IIncentiveRewardClauseRuleRecordService;
import com.jhgsys.internal.incentive.service.IIncentiveRewardClauseRuleService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 奖励条款规则 Service实现
 *
 * @author zjb
 * @date 2024-08-30 09:51:15
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IncentiveRewardClauseRuleServiceImpl extends ServiceImpl<IncentiveRewardClauseRuleMapper, IncentiveRewardClauseRule> implements IIncentiveRewardClauseRuleService {

    @Autowired
    private IIncentiveRewardClauseRuleRecordService incentiveRewardClauseRuleRecordService;


    /**
     * 查询（分页）
     * @author zjb
     * @date 2024-08-30 09:51:15
     * @param request QueryRequest
     * @param incentiveRewardClauseRule incentiveRewardClauseRule
     * @return IPage<IncentiveRewardClauseRule>
    */
    @Override
    public IPage<IncentiveRewardClauseRule> findIncentiveRewardClauseRulePage(QueryRequest request, IncentiveRewardClauseRule incentiveRewardClauseRule) {
        LambdaQueryWrapper<IncentiveRewardClauseRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveRewardClauseRule::getDeletedFlg, 0);
        // TODO 设置查询条件
        if(StringUtils.isNotBlank(incentiveRewardClauseRule.getClauseId())){
            queryWrapper.eq(IncentiveRewardClauseRule::getClauseId, incentiveRewardClauseRule.getClauseId());
        }
        if(StringUtils.isNotBlank(incentiveRewardClauseRule.getClauseNo())){
            queryWrapper.like(IncentiveRewardClauseRule::getClauseNo, incentiveRewardClauseRule.getClauseNo());
        }
        if(StringUtils.isNotBlank(incentiveRewardClauseRule.getTenantId())){
            queryWrapper.eq(IncentiveRewardClauseRule::getTenantId, incentiveRewardClauseRule.getTenantId());
        }
        if(StringUtils.isNotBlank(incentiveRewardClauseRule.getClauseContent())){
            queryWrapper.like(IncentiveRewardClauseRule::getClauseContent, incentiveRewardClauseRule.getClauseContent());
        }
        if(StringUtils.isNotBlank(incentiveRewardClauseRule.getStatus())){
            queryWrapper.eq(IncentiveRewardClauseRule::getStatus, incentiveRewardClauseRule.getStatus());
        }
        queryWrapper.orderByAsc(IncentiveRewardClauseRule::getClauseLevel);
        Page<IncentiveRewardClauseRule> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zjb
     * @date 2024-08-30 09:51:15
     * @param incentiveRewardClauseRule incentiveRewardClauseRule
     * @return List<IncentiveRewardClauseRule>
     */
    @Override
    public List<IncentiveRewardClauseRule> findIncentiveRewardClauseRuleList(IncentiveRewardClauseRule incentiveRewardClauseRule) {
	    LambdaQueryWrapper<IncentiveRewardClauseRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveRewardClauseRule::getDeletedFlg, 0);
		// TODO 设置查询条件
        if(StringUtils.isNotBlank(incentiveRewardClauseRule.getClauseNo())){
            queryWrapper.eq(IncentiveRewardClauseRule::getClauseNo,incentiveRewardClauseRule.getClauseNo());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zjb
    * @date 2024-08-30 09:51:15
    * @param id
    * @return IncentiveRewardClauseRule
    */
    @Override
    public IncentiveRewardClauseRule findIncentiveRewardClauseRuleById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zjb
     * @date 2024-08-30 09:51:15
     * @param incentiveRewardClauseRule incentiveRewardClauseRule
     */
    @Override
    @Transactional
    public void createIncentiveRewardClauseRule(IncentiveRewardClauseRule incentiveRewardClauseRule) {
        incentiveRewardClauseRule.setCreatedTime(new Date());
        this.save(incentiveRewardClauseRule);
        IncentiveRewardClauseRuleRecord record = new IncentiveRewardClauseRuleRecord();
        record.setClauseRuleId(incentiveRewardClauseRule.getClauseRuleId());
        record.setCreatedBy(incentiveRewardClauseRule.getCreatedBy());
        record.setChangeTime(new Date());
        record.setAfterJson(JSON.toJSONString(incentiveRewardClauseRule));
        record.setChangeType("1");
        incentiveRewardClauseRuleRecordService.createIncentiveRewardClauseRuleRecord(record);
    }

    /**
     * 修改
     * @author zjb
     * @date 2024-08-30 09:51:15
     * @param incentiveRewardClauseRule incentiveRewardClauseRule
     */
    @Override
    @Transactional
    public void updateIncentiveRewardClauseRule(IncentiveRewardClauseRule incentiveRewardClauseRule) {
        incentiveRewardClauseRule.setUpdatedTime(new Date());
        this.saveOrUpdate(incentiveRewardClauseRule);
        IncentiveRewardClauseRule oldRule = this.findIncentiveRewardClauseRuleById(incentiveRewardClauseRule.getClauseRuleId());
        if(incentiveRewardClauseRule.getInsertFlg()){
            IncentiveRewardClauseRuleRecord record = new IncentiveRewardClauseRuleRecord();
            record.setClauseRuleId(incentiveRewardClauseRule.getClauseRuleId());
            record.setCreatedBy(incentiveRewardClauseRule.getCreatedBy());
            record.setChangeTime(new Date());
            record.setAfterJson(JSON.toJSONString(incentiveRewardClauseRule));
            record.setBeforeJson(JSON.toJSONString(oldRule));
            record.setChangeType("2");
            incentiveRewardClauseRuleRecordService.createIncentiveRewardClauseRuleRecord(record);
        }
    }

    /**
     * 逻辑删除
     * @author zjb
     * @date 2024-08-30 09:51:15
     * @param incentiveRewardClauseRule incentiveRewardClauseRule
     * @param incentiveRewardClauseRuleIds incentiveRewardClauseRuleIds
     */
    @Override
    @Transactional
    public void deleteIncentiveRewardClauseRule(IncentiveRewardClauseRule incentiveRewardClauseRule,String incentiveRewardClauseRuleIds) {
        List<String> list = Arrays.asList(incentiveRewardClauseRuleIds.split(StringPool.COMMA));
        this.baseMapper.update(incentiveRewardClauseRule,new LambdaQueryWrapper<IncentiveRewardClauseRule>().in(IncentiveRewardClauseRule::getClauseRuleId,list));
	}

    /**
    * 物理删除
    * @author zjb
    * @date 2024-08-30 09:51:15
    * @param incentiveRewardClauseRuleIds incentiveRewardClauseRuleIds
    */
    @Override
    @Transactional
    public void physicallyDeleteIncentiveRewardClauseRule(String incentiveRewardClauseRuleIds) {
        List<String> list = Arrays.asList(incentiveRewardClauseRuleIds.split(StringPool.COMMA));
        LambdaQueryWrapper<IncentiveRewardClauseRule> wapper = new LambdaQueryWrapper<>();
        wapper.in(IncentiveRewardClauseRule::getClauseRuleId,list);
        this.remove(wapper);
    }
}
